package com.lhnstart.learn.bases.object_;

import com.oracle.xmlns.internal.webservices.jaxws_databinding.SoapBindingUse;

/**
 * 自定义equals
 *
 * @author lhn
 * @date 2023/06/09 10:52
 * @since 1.0.0
 **/
public class EqualsExercise01 {

    public static void main(String[] args) {
        Person person = new Person("jack", 10, '男');
        Person person1 = new Person("jack", 10, '男');
        System.out.println(person.equals(person1));
    }

}

class Person{ //extends Object
    private String name;
    private int age;
    private char gender;

    // 重写 object 方法


    @Override
    public boolean equals(Object obj) {
        // 判断如果比较的两个对象时同一个对象 则直接放回 true
        if (this == obj) {
            return true;
        }
        // 类型判断
        if (obj instanceof Person) {
            // 进行向下转型
            Person p = (Person) obj;
            return this.name.equals(p.getName()) && this.age == p.age && this.gender == p.gender;
        }
        return false;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}